<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 16, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls( "base.SiteResourceBase" );

/**
 * Class keeping all resoucrce of a site
 *
 */
class SiteResource extends SiteResourceBase {
	
	/**
	 * Constructor
	 * 
	 * @param string $application
	 * @param string $action
	 * @param string $task
	 * @param string $option
	 * @return SiteResource
	 */
	function __construct($application = null, $action = null, $task = null, $option = null) {
		parent::__construct ( $application, $action, $task, $option );
	}

	/**
	 * Get the link of this resource
	 *
	 */
	function getLink($withIndex = true, $withExtend = true, $encodeUrl = true) {
		if ($withIndex) {
			if(SystemSite::isBackEnd()) {
				if (strpos($link, SYS_ADMIN_URL) === false ) {
					$link = SYS_ADMIN_URL;
				}
			} else {
				if (strpos($link, SYS_ROOT_URL) === false ) {
					$link = SYS_ROOT_URL;
				}
			}
		}
		if (SYS_WORK_ZONE == 'back') {
			$arrLink = array();
			$size = count($this->_arrProperties);
			foreach ($this->_mapRequest as $proName=>$key) {
				$val = $this->$proName;
				if (!empty($val)) {
					if ($encodeUrl) {
					$val = urlencode($val);
					}
					$arrLink[] = "{$val}";
				}
			}
			$link .= implode("/", $arrLink);
			$link .= "/";
			
			$arrLink=array();
			if ($withExtend) {
				foreach ($this->_extend as $key=>$value) {
					if ($encodeUrl) {
					$val = @urlencode($value);
					}
					else {
						$val = $value;
					}
					$arrLink[] = "{$key}={$val}";
				}
			}
			if(!empty($arrLink)) {
				$link .= "?".implode("&", $arrLink);
			}
		} else {
			if (!$this->_extend['lang']) {
				$this->_extend['lang'] = SystemSite::getLanguage();
			}
			// Bind link with partten 'lang/app/act/task';
			$arrLink=array();
			if($this->_extend['lang']) {
				$arrLink[] = $this->_extend['lang'];
				//unset($this->_extend['lang']);
			}
			if($this->_extend['model_name']) {
				$arrLink[] = $this->_extend['model_name'];
				$ignoreUserName = true;
			}
			foreach ($this->_mapRequest as $proName=>$key) {
				if ($ignoreUserName && $key == 'app' && $this->application == 'model') {
					continue;
				}
				$val = $this->$proName;
				if (!empty($val)) {
					if ($encodeUrl) {
					$val = urlencode($val);
					}
					$arrLink[] = "{$val}";
				}
			}
			$link .= implode("/", $arrLink);
//			$link .= "/";
			
			$arrLink=array();
			if ($withExtend) {
				foreach ($this->_extend as $key=>$value) {
					if (($key == 'lang' || ($key == 'model_name' && $ignoreUserName))) {
						continue;
					}
					if ($encodeUrl) {
						if (is_array($value) && count($value) > 0) {
							foreach ($value as $val) {
								$val = urlencode($val);
								$arrLink[] = "{$key}[]:{$val}";
							}
							continue;
						}
						
						if ($key != 'lang') {
							$val = urlencode($value);
						}
						else {
							$val = $value;
						}
						
						if(!is_numeric($key)) {
							$arrLink[] = "$key:{$val}";
						} else {
							$arrLink[] = "{$val}";
						}
						
					}
				}
			}
			if(!empty($arrLink)) {
				$link .= "/".implode("/", $arrLink);
			}
		}
		
		return $link;
	}	
}
?>